package com.example.zyfypt_206shj.model;

import com.example.zyfypt_206shj.bean.RegInfo;
import com.example.zyfypt_206shj.bean.UserInfo;
import com.example.zyfypt_206shj.iface.LoginIface;
import com.example.zyfypt_206shj.iface.LoginListener;
import com.example.zyfypt_206shj.iface.RegIface;
import com.example.zyfypt_206shj.iface.RegListener;
import com.example.zyfypt_206shj.service.LoginService;
import com.example.zyfypt_206shj.service.RegService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Raindy on 2017/4/14.
 */

public class RegModel implements RegIface {
    private Retrofit retrofit;
    private String BASEURL="http://amicool.neusoft.edu.cn/";
    private RegService regService;

    public RegModel() {
        //使用Retrofit-1
        retrofit = new Retrofit.Builder().baseUrl(BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getRegInfo(String username, String password, String tel, final RegListener listenser) {
        regService = retrofit.create(RegService.class);
        Call<String> call = regService.reg(username, password, tel);
        //使用Retrofit-3
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if(response.isSuccessful() && response.body()!=null){
                    listenser.onResponse(response.body());
                }else {
                    listenser.onFail("onResponse fail.");
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                listenser.onFail(t.toString());
            }
        });
    }
}

